﻿package sparrowGui
{
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.events.EventDispatcher;
	import flash.events.MouseEvent;
	
	import sparrowGui.item.Item;
	import sparrowGui.item.RadioItem;
	import sparrowGui.utils.PubFun;

	/**
	 * 跟RadioGroup的区别在于此单选组不会自动创建项放到容器里,
	 * 而是直接控制场景里的显示对象,把对象变成五状态按钮.
	 * @author Pelephone
	 */	
	public class RadioGroup2 extends EventDispatcher
	{
//		protected var parseFun:Function;			// 解析子项的方法
		
		protected var btns:Array;
		private var _selectedIndexs:Array;
		private var _itemsData:Array;
		private var _multiSelect:Boolean = false;	//是否可选多行
		private var _mustSelect:Boolean = true;		//是否必须选一项
		private var _visible:Boolean = true;
		private var _enable:Boolean = true;			//是否激活此控件
		
		public function RadioGroup2(data:Array, butons:Array)
		{
			btns = [];
			_selectedIndexs = [];
			_itemsData = [];
//			parseFun = itemFun;
//			if(itemFun!=null) addEventListener(RadioGroup.ITEM_UPDATE,itemFun);
//			super(data, itemSkinCla, vars);
			var itm:Item;
			for(var i:int=0;i<butons.length;i++){
				if(!data || i>=data.length)
					itm = addItemBtn(null,butons[i]);
				else itm = addItemBtn(data[i],butons[i]);
				itm.index = i;
			}
			
			// 默认选择第一项
			if(_mustSelect) addSelectedIndex(0);
		}
		
		public function update(datas:Array):void
		{
			_itemsData = datas;
			for(var i:int=0;i<_itemsData.length;i++){
				if(!btns || !btns[i]) continue;
				getItemByIndex(i).update(datas[i]);
				parseItem(datas[i],getItemByIndex(i));
			}
		}
		
		// 解释子项数据皮肤方法
		protected function parseItem(data:Object, item:Item):void
		{
			if(hasEventListener(RadioEvent.ITEM_UPDATE)){
				dispatchEvent(new RadioEvent(RadioEvent.ITEM_UPDATE,item));
				return;
			}
			if(data is Array){
				RadioGroup.parseBySkinPrefix(data,item.itemSkin);
			}else{
				RadioGroup.parseBySkinName(data,item.itemSkin);
			}
		}
		
		// 获取项,通过在数组里的位置
		public function getItemByIndex(id:int):Item
		{
			return btns[id] as Item;
		}
		
		public function getItemData(id:int):Object
		{
			return getItemByIndex(id).itemData;
		}
		
		/**
		 * 列表项的鼠标事件
		 */
		protected function onItemMouseEvt(e:MouseEvent):void
		{
			var _item:Item = e.target as Item;
			if(!_item) return;
			switch(e.type){
				case MouseEvent.CLICK:
					addSelectedIndex(btns.indexOf(e.target));
					break;
			}
		}
		
		// 添加按钮
		public function addBtn(disp:Sprite):Item
		{
			var btn:Item = PubFun.changeSP2Item(disp,RadioItem);
			btn.addEventListener(MouseEvent.CLICK,onItemMouseEvt);
			btns.push(btn);
			return btn;
		}
		
		//
		public function addItemBtn(data:String,disp:Sprite,isHtml:Boolean=false):Item
		{
			var item:Item = addBtn(disp);
			item.update(data);
			dispatchEvent(new RadioEvent(RadioEvent.ITEM_UPDATE,item));
			return item;
		}
		
		/**
		 * 设置某项为选中状态
		 */
		public function addSelectedIndex(value:int):void
		{
			// 如果至少得选一项为真
			if(_mustSelect && _selectedIndexs.indexOf(value)>=0
				&& _selectedIndexs.length==1){
				dispatchEvent(new RadioEvent(RadioEvent.ITEM_SELECT,getItemByIndex(value)));
				return;
			}
			
			var ind:int = _selectedIndexs.indexOf(value);
			if(ind<0){
				if(_multiSelect)
					_selectedIndexs.push(value);
				else
					_selectedIndexs = [value];
			}else{
				if(_multiSelect)
					_selectedIndexs.splice(ind,1);
				else
					_selectedIndexs = []
			}
			
			selectFresh();
			dispatchEvent(new RadioEvent(RadioEvent.ITEM_SELECT,getItemByIndex(value)));
		}
		
		/**
		 * 单多选之后按_selectedIndexs数组重刷显示
		 */
		protected function selectFresh():void
		{
			for(var i:int=0;i<btns.length;i++){
				var oItem:Item = btns[i] as Item;
				if(!oItem) continue;
				if(_selectedIndexs.indexOf(i)>=0){
					oItem.selected=true;
				}else
					oItem.selected=false;
			}
		}
		
		/**
		 * 全选
		 * 一定要是多选打开才能全选,否则无效果
		 */		
		public function selectAll():void
		{
			if(!_multiSelect) return;
			for(var i:int=0;i<btns.length;i++){
				var oItem:Item = btns[i] as Item;
				if(!oItem) continue;
				_selectedIndexs.push(oItem.index);
				oItem.selected=true;
			}
		}
		
		/**
		 * 全部取消选择
		 */
		public function unSelectAll():void
		{
			_selectedIndexs = [];
			
			for(var i:int=0;i<btns.length;i++){
				var oItem:Item = btns[i] as Item;
				oItem.selected=false;
			}
		}
		
		public function get selectedIndexs():Array
		{
			return _selectedIndexs;
		}
		
		/**
		 * 获取已选中的项数据
		 * @return 
		 */		
		public function getSelectedDates():Array
		{
			var resArr:Array=[];
			for(var i:int=0;i<_itemsData.length;i++){
				if(_selectedIndexs.indexOf(i)>=0) resArr.push(_itemsData[i]);
			}
			return resArr;
		}
		
		//返回选的第一个项,单选时有用
		public function getFirstItem():Item
		{
			if(!_selectedIndexs.length) return null;
			var id:int = _selectedIndexs[0];
			return getItemByIndex(id);
		}
		
		//返回选的第一个项的数据,单选时有用
		public function getFirstData():Object
		{
			var it:Item = getFirstItem();
			if(it) return it.itemData;
			else return null;
		}
		
		public function removeItem(id:int):void
		{
			if(_selectedIndexs.indexOf(id)<0) return;
			var it:Item = getItemByIndex(id);
			it.selected = false;
			it.removeEventListener(MouseEvent.CLICK,onItemMouseEvt);
			btns.splice(id,1);
			_selectedIndexs.splice(_selectedIndexs.indexOf(id),1);
		}
		
		public function removeAll():void
		{
			for each(var it:Item in btns){
				it.removeEventListener(MouseEvent.CLICK,onItemMouseEvt);
				it.dispose();
				if(it.parent) it.parent.removeChild(it);
				it = null;
			}
			_selectedIndexs = [];
		}
		
		public function set enable(value:Boolean):void 
		{
			for each(var it:Item in btns){
				it.enabled = value;
			}
			_enable = value;
		}
		
		public function get enable():Boolean 
		{
			return _enable;
		}
		
		public function get itemsData():Array
		{
			return _itemsData;
		}
		
		public function set itemsData(value:Array):void
		{
			_itemsData = value;
		}

		public function get multiSelect():Boolean
		{
			return _multiSelect;
		}

		public function set multiSelect(value:Boolean):void
		{
			_multiSelect = value;
		}

		public function get mustSelect():Boolean
		{
			return _mustSelect;
		}
		
		public function get visible():Boolean
		{
			return _visible;
		}
		
		public function set visible(value:Boolean):void
		{
			_visible = value;
			for each(var disp:DisplayObject in btns){
				disp.visible = value;
			}
		}

		public function set mustSelect(value:Boolean):void
		{
			_mustSelect = value;
		}
		
		/**
		 * 获取所有按钮项,<Item>
		 * @return 
		 */
		public function get allBtnItem():Array
		{
			return btns;
		}
		
		public function dispose():void
		{
			removeAll();
			btns 			= null;
			_itemsData 		= null;
			_selectedIndexs = null;
		}
	}
}